﻿using Apps.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Apps.BLL.Report
{
    public class ReportBLL<T>  where T : class
    {
        //public static List<V_STATS_RC_WEIGHTING> GetStatsrRcWeighting(Expression<Func<V_STATS_RC_WEIGHTING, bool>> whereLambda)
        //{
        //    using (var db = new DBContainer())
        //    {
        //        return db.V_STATS_RC_WEIGHTING.Where(whereLambda.Compile()).ToList();
        //    }
        //}

        //public static List<V_RC_WEIGHING_RECORD> GetRcWeighingRecord(Expression<Func<V_RC_WEIGHING_RECORD, bool>> whereLambda)
        //{
        //    using (var db = new DBContainer())
        //    {
        //        return db.V_RC_WEIGHING_RECORD.Where(whereLambda.Compile()).ToList();
        //    }
        //}

        public static List<T> GetReportDataList(Expression<Func<T, bool>> whereLambda)
        {
            using (var db = new DBContainer())
            {
                return db.Set<T>().Where(whereLambda.Compile()).ToList();
            }
        }


        public static List<V_STATS_RL_SML_MONTH> GetYearDateDT(Expression<Func<V_STATS_RL_SML_MONTH, bool>> whereLambda)
        {
            using (var db = new DBContainer())
            {
                var query = db.V_STATS_RL_SML_MONTH.Where(whereLambda.Compile());
                var list = (from a in query
                            group a by new { a.STATS_YEAR, a.STATS_MONTH } into b
                            select new V_STATS_RL_SML_MONTH
                            {
                                STATS_DATE = Convert.ToDateTime(b.Key.STATS_YEAR.ToString() + "-" + (b.Key.STATS_MONTH < 10 ? "0" + b.Key.STATS_MONTH : b.Key.STATS_MONTH.ToString()) + "-" + "01"),
                                STATS_YEAR = b.Key.STATS_YEAR,
                                STATS_MONTH = b.Key.STATS_MONTH,
                                HY = b.Sum(c => c.HY),
                                BB = b.Sum(c => c.BB),
                                ZB = b.Sum(c => c.ZB),
                                QY = b.Sum(c => c.QY),
                                JZA = b.Sum(c => c.JZA),
                                JZB = b.Sum(c => c.JZB),
                                XJ = b.Sum(c => c.XJ)
                            }).OrderBy(t => t.STATS_MONTH);

                return list.ToList();
            }
        }


        public static List<V_STATS_RL_PLAN_RESULT> GetRl_PlanDReportDataList(Expression<Func<V_STATS_RL_PLAN_RESULT, bool>> whereLambda)
        {
            using (var db = new DBContainer())
            {
                var query = db.V_STATS_RL_PLAN_RESULT.Where(whereLambda.Compile());
                var list = (from a in query
                            group a by new { a.CREATEDATE } into b
                            select new V_STATS_RL_PLAN_RESULT
                            { 
                                CREATEDATE = b.Key.CREATEDATE,
                                TOTAL = b.Sum(c => c.TOTAL),
                                MT = b.Sum(c => c.MT),
                                MAD = b.Sum(c => c.MAD),
                                AAD = b.Sum(c => c.AAD),
                                AD = b.Sum(c => c.AD),
                                AAR = b.Sum(c => c.AAR),
                                VAD = b.Sum(c => c.VAD),
                                VD = b.Sum(c => c.VD),
                                VAR = b.Sum(c => c.VAR),
                                VDAF = b.Sum(c => c.VDAF),
                                FCAD = b.Sum(c => c.FCAD),
                                FCAR = b.Sum(c => c.FCAR),
                                CRC = b.Sum(c => c.CRC),
                                STAD = b.Sum(c => c.STAD),
                                STD = b.Sum(c => c.STD),
                                STAR = b.Sum(c => c.STAR),
                                STDAF = b.Sum(c => c.STDAF),
                                QBAD = b.Sum(c => c.QBAD),
                                QGRAD = b.Sum(c => c.QGRAD),
                                QGRD = b.Sum(c => c.QGRD),
                                QNETAR_MJ = b.Sum(c => c.QNETAR_MJ),
                                QNETAR_KCAL = b.Sum(c => c.QNETAR_KCAL),
                                HAD = b.Sum(c => c.HAD)
                            }).OrderBy(t => t.CREATEDATE);

                return list.ToList();
            }
        }


        public static List<V_STATS_RL_PLAN_RESULT> GetRl_PlanYReportDataList(Expression<Func<V_STATS_RL_PLAN_RESULT, bool>> whereLambda)
        {
            using (var db = new DBContainer())
            { 
                var query = db.V_STATS_RL_PLAN_RESULT.Where(whereLambda.Compile());
                var list = (from a in query
                           group a by new { a.STATS_YEAR, a.STATS_MONTH } into b 
                           select new V_STATS_RL_PLAN_RESULT
                           {
                               STATS_YEAR = b.Key.STATS_YEAR,
                               STATS_MONTH = b.Key.STATS_MONTH,
                               CREATEDATE = String.Format("{0}-{1}", b.Key.STATS_YEAR, b.Key.STATS_MONTH),
                               TOTAL = b.Sum(c => c.TOTAL),
                               MT = b.Sum(c => c.MT),
                               MAD = b.Sum(c => c.MAD),
                               AAD = b.Sum(c => c.AAD),
                               AD = b.Sum(c => c.AD),
                               AAR = b.Sum(c => c.AAR),
                               VAD = b.Sum(c => c.VAD),
                               VD = b.Sum(c => c.VD),
                               VAR = b.Sum(c => c.VAR),
                               VDAF = b.Sum(c => c.VDAF),
                               FCAD = b.Sum(c => c.FCAD),
                               FCAR = b.Sum(c => c.FCAR),
                               CRC = b.Sum(c => c.CRC),
                               STAD = b.Sum(c => c.STAD),
                               STD = b.Sum(c => c.STD),
                               STAR = b.Sum(c => c.STAR),
                               STDAF = b.Sum(c => c.STDAF),
                               QBAD = b.Sum(c => c.QBAD),
                               QGRAD = b.Sum(c => c.QGRAD),
                               QGRD = b.Sum(c => c.QGRD),
                               QNETAR_MJ = b.Sum(c => c.QNETAR_MJ),
                               QNETAR_KCAL = b.Sum(c => c.QNETAR_KCAL),
                               HAD = b.Sum(c => c.HAD) 
                           }).OrderBy(t => t.STATS_YEAR).ThenBy(t => t.STATS_MONTH);
       
                return list.ToList();
            }
        }

        public static List<V_STATS_RL_PLAN_RESULT> GetRl_PlanMReportDataList(Expression<Func<V_STATS_RL_PLAN_RESULT, bool>> whereLambda)
        {
            using (var db = new DBContainer())
            {
                var query = db.V_STATS_RL_PLAN_RESULT.Where(whereLambda.Compile());
                var list = (from a in query
                            group a by new { a.CREATEDATE, a.TEAM_NAME, a.CREW_NAME, a.TEST_CODE } into b
                            select new V_STATS_RL_PLAN_RESULT
                            { 
                                CREATEDATE = b.Key.CREATEDATE,
                                TEAM_NAME = b.Key.TEAM_NAME,
                                CREW_NAME = b.Key.CREW_NAME,
                                TEST_CODE = b.Key.TEST_CODE,
                                TOTAL = b.Sum(c => c.TOTAL),
                                MT = b.Sum(c => c.MT),
                                MAD = b.Sum(c => c.MAD),
                                AAD = b.Sum(c => c.AAD),
                                AD = b.Sum(c => c.AD),
                                AAR = b.Sum(c => c.AAR),
                                VAD = b.Sum(c => c.VAD),
                                VD = b.Sum(c => c.VD),
                                VAR = b.Sum(c => c.VAR),
                                VDAF = b.Sum(c => c.VDAF),
                                FCAD = b.Sum(c => c.FCAD),
                                FCAR = b.Sum(c => c.FCAR),
                                CRC = b.Sum(c => c.CRC),
                                STAD = b.Sum(c => c.STAD),
                                STD = b.Sum(c => c.STD),
                                STAR = b.Sum(c => c.STAR),
                                STDAF = b.Sum(c => c.STDAF),
                                QBAD = b.Sum(c => c.QBAD),
                                QGRAD = b.Sum(c => c.QGRAD),
                                QGRD = b.Sum(c => c.QGRD),
                                QNETAR_MJ = b.Sum(c => c.QNETAR_MJ),
                                QNETAR_KCAL = b.Sum(c => c.QNETAR_KCAL),
                                HAD = b.Sum(c => c.HAD)
                            }).OrderBy(t => t.CREATEDATE).ThenBy(t => t.TEAM_NAME).ThenBy(t => t.TEST_CODE);

                return list.ToList();
            }
        }
    }
}
